/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 magic_mirror.cpp

	$Header: /heroes4/magic_mirror.cpp $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "magic_mirror.h"

#include "battlefield.h"
#include "can_cast.h"
#include "combat_creature.h"
#include "combat_spell.h"
#include "combat_spell_registration.h"
#include "external_string.h"
#include "replace_keywords.h"
#include "spell.h"
#include "spell_properties.h"

// -----------------------------------------------------------------
// mirror a spell after all animation finishes
// -----------------------------------------------------------------
t_mirror_spell_action::t_mirror_spell_action( t_combat_creature_ptr creature, 
											  t_combat_spell* spell, int power )
{
	m_creature = creature;
	m_spell    = spell;
	m_power	   = power;
}

// -----------------------------------------------------------------
// mirror a spell after all animation finishes
// -----------------------------------------------------------------
void t_mirror_spell_action::operator()()
{
	t_window_handler handler;
	t_battlefield&		battlefield = m_creature->get_battlefield();

	t_combat_action_message message( *m_creature, m_spell->get_action_text( true ) );
	handler = new t_mirror_spell_handler( m_creature, m_spell, m_power );
	battlefield.start_animation( *m_creature, k_spell_magic_mirror, handler, message );
}

// -----------------------------------------------------------------
// mirror a spell after mirror animation plays
// -----------------------------------------------------------------
t_mirror_spell_handler::t_mirror_spell_handler( t_combat_creature_ptr creature, 
											    t_combat_spell*       spell,
												int					  power )
{
	m_creature = creature;
	m_spell = spell;
	m_power = power;
}

// -----------------------------------------------------------------
// mirror a spell after mirror animation plays
// -----------------------------------------------------------------
void t_mirror_spell_handler::operator()( t_window* window )
{
	t_battlefield&		battlefield = m_creature->get_battlefield();

	if (m_creature->get_number() > 0)
		m_spell->execute_mirror_spell( m_creature, (m_power + 1) / 2);
	battlefield.update_state();
}

extern t_external_string const k_combat_action_cause_effect;

// ----------------------------------------------------------
// Action message for an attack-caused effect
// ----------------------------------------------------------
static std::string get_effect_text( t_spell effect )
{
	return replace_keywords( k_combat_action_cause_effect, "%spell", 
		                     get_short_spell_name( effect ) );
}

void cast_and_mirror( t_combat_creature* attacker, t_combat_creature* defender,
					  t_spell spell, std::string text )
{
	if (text.empty())
		text = get_effect_text( spell );

	t_combat_action_message message( *attacker, text );
	bool					magic_mirror = defender->has_magic_mirror();
	t_battlefield&			battlefield = attacker->get_battlefield();
	int						power = attacker->get_spell_power( spell );

	if (battlefield.cast_spell( *attacker, *defender, spell, message, power )
		&& magic_mirror && attacker->get_number() > 0
		&& can_affect( spell, *attacker ))
	{
		t_combat_spell_ptr spell_ptr = create_combat_spell( battlefield, spell ); 

		spell_ptr->set_caster( attacker );
		battlefield.add_action( new t_mirror_spell_action( defender, spell_ptr, power ));
	}
}
